package com.cloudpolice.door.controller;

import com.cloudpolice.door.feignInterface.EquipmentInterFace;
import com.cloudpolice.door.feignInterface.EquipmentMessageInterFace;
import com.cloudpolice.door.model.EquipmentSiteModel;
import com.google.gson.Gson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lh.model.ResultVO;
import lh.model.ResultVOPage;
import lh.units.ResultStruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @author ：梁昊，后端工程师：flyman5，前端工程师：孙耀
 * @create 2020-04-01 22:33
 * @function
 * @editLog
 */
@RestController
@RequestMapping("/equipmentMessageFeignController")
@Api(value = "设备粒子层", description = "设备基本与关联信息方法")
public class EquipmentMessageFeignController {
    @Autowired
    EquipmentMessageInterFace equipmentMessageInterFace;
    @Autowired
    EquipmentInterFace equipmentInterFace;

    @Autowired
    Gson gson;

    /**
     * 根据社区ID或区域ID得到消息与通知信息，方法ID：SE2020040200091279772685E0166F9
     *
     * @param id          表vou_EquipmentMessage,字段名id:
     * @param communityId 表vou_EquipmentMessage,字段名communityId:社区ID
     * @param periodId    表vou_EquipmentMessage,字段名periodId:社区区域ID
     * @param page        当前页数
     * @param limit       每页条数
     * @return 设备基本信息
     */
    @ApiOperation(value = "根据社区ID或区域ID得到消息与通知信息,方法ID：SE2020040200091279772685E0166F9", notes = "设备基本信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "String"),
            @ApiImplicitParam(name = "communityId", value = "社区ID", dataType = "String", required = true),
            @ApiImplicitParam(name = "periodId", value = "社区区域ID", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "当前页数", dataType = "String"),
            @ApiImplicitParam(name = "limit", value = "每页条数", dataType = "String")
    })
    @PostMapping("/selectEquipmentMessageByCom")
    public ResultVOPage selectEquipmentMessageByCom(@RequestParam(value = "id", required = false) String id
            , @RequestParam(value = "communityId") String communityId
            , @RequestParam(value = "periodId", required = false) String periodId
            , @RequestParam(value = "page", defaultValue = "1") int page
            , @RequestParam(value = "limit", defaultValue = "10") int limit) {
        ResultVOPage resultValue = equipmentMessageInterFace.selectEquipmentMessageByCom(id, communityId, periodId
                , page
                , limit);
        return resultValue;
    }

    /**
     * 根据社区ID或区域ID得到消息与通知信息，方法ID：SE202004020015307960B88322AD120
     *
     * @param id 表vou_EquipmentMessage,字段名id:
     * @return 设备基本信息
     */
    @ApiOperation(value = "根据ID得到消息与通知信息,方法ID：SE202004020015307960B88322AD120", notes = "设备基本信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "String", required = true)
    })
    @PostMapping("/selectEquipmentMessageById")
    public ResultVO selectEquipmentMessageById(@RequestParam(value = "id") String id) {
        return equipmentMessageInterFace.selectEquipmentMessageById(id);
    }

    /**
     * 审批通知，方法ID：UP20200401232747641D56307B2F8CC
     *
     * @param id        , Where字段
     * @param audioSign 是否审批通过
     * @return 影响条件
     */
    @ApiOperation(value = "修改门禁机消息与通知,方法ID：UP20200401232747641D56307B2F8CC", notes = "影响条件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "String", required = true)
            , @ApiImplicitParam(name = "audioSign", value = "是否审批通过", dataType = "Boolean")
    })
    @PostMapping("/updateEquipmentMessageAudio")
    public ResultVO updateEquipmentMessageAudio(@RequestParam(value = "id") String id
            , @RequestParam(value = "audioSign", required = false) boolean audioSign) {
        final int i = equipmentMessageInterFace.updateEquipmentMessage(id
                , null, null
                , null, -1, null
                , null, audioSign, -1
                , null, null
                , -1, -1, -1, -1);
        if (i > 0)
            return ResultStruct.success(i);
        else
            return ResultStruct.error(ResultVO.class);
    }

    /**
     * 修改门禁机消息与通知，方法ID：UP20200401232747641D56307B2F8CC
     *
     * @param id          , Where字段
     * @param message     消息
     * @param messageType 消息类型（0：一般消息、1：通知）
     * @param createName  发布者名称
     * @param createTime  通知日期
     * @param audioSign   是否审批通过
     * @param sortNo      出现顺序
     * @param beginDate   发布开始日期（不包括当天）
     * @param endDate     发布结束日期（不包括当天）
     * @return 影响条件
     */
    @ApiOperation(value = "修改门禁机消息与通知,方法ID：UP20200401232747641D56307B2F8CC", notes = "影响条件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "String", required = true)
            , @ApiImplicitParam(name = "message", value = "消息", dataType = "String")
            , @ApiImplicitParam(name = "messageType", value = "消息类型（0：一般消息、1：通知）", dataType = "Integer")
            , @ApiImplicitParam(name = "createName", value = "发布者名称", dataType = "String")
            , @ApiImplicitParam(name = "createTime", value = "通知日期", dataType = "Date")
            , @ApiImplicitParam(name = "audioSign", value = "是否审批通过", dataType = "Boolean")
            , @ApiImplicitParam(name = "sortNo", value = "出现顺序", dataType = "Integer")
            , @ApiImplicitParam(name = "beginDate", value = "发布开始日期（不包括当天）", dataType = "Date")
            , @ApiImplicitParam(name = "endDate", value = "发布结束日期（不包括当天）", dataType = "Date")
            , @ApiImplicitParam(name = "colorR", value = "R颜色值", dataType = "String", defaultValue = "255", required = true)
            , @ApiImplicitParam(name = "colorG", value = "G颜色值", dataType = "String", defaultValue = "255", required = true)
            , @ApiImplicitParam(name = "colorB", value = "B颜色值", dataType = "String", defaultValue = "255", required = true)
            , @ApiImplicitParam(name = "fontSize", value = "文字大小", dataType = "String", defaultValue = "15", required = true)
    })
    @PostMapping("/updateEquipmentMessage")
    public ResultVO updateEquipmentMessage(@RequestParam(value = "id") String id
            , @RequestParam(value = "message", required = false) String message
            , @RequestParam(value = "messageType", required = false) int messageType
            , @RequestParam(value = "createName", required = false) String createName
            , @RequestParam(value = "createTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date createTime
            , @RequestParam(value = "audioSign", required = false) boolean audioSign
            , @RequestParam(value = "sortNo", required = false) int sortNo
            , @RequestParam(value = "beginDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date beginDate
            , @RequestParam(value = "endDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate
            , @RequestParam(value = "colorR", defaultValue = "-1") int colorR
            , @RequestParam(value = "colorG", defaultValue = "-1") int colorG
            , @RequestParam(value = "colorB", defaultValue = "-1") int colorB
            , @RequestParam(value = "fontSize", defaultValue = "-1") int fontSize
    ) {
        if (beginDate.compareTo(endDate) > 0)
            return ResultStruct.error("发布结束日期不能小于发布开始日期", ResultVO.class);
        final int i = equipmentMessageInterFace.updateEquipmentMessage(id
                , null, null
                , message, messageType, createName
                , createTime, audioSign, sortNo
                , beginDate, endDate
                , colorR, colorG, colorB, fontSize);
        if (i > 0)
            return ResultStruct.success(i);
        else
            return ResultStruct.error(ResultVO.class);
    }

    /**
     * 删除门禁机消息与通知，方法ID：DE202004012319246865629E542A7DE
     *
     * @param id
     * @return 影响条件
     */
    @ApiOperation(value = "删除门禁机消息与通知,方法ID：DE202004012319246865629E542A7DE", notes = "影响条件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "String")
    })
    @PostMapping("/deleteEquipmentMessage")
    public ResultVO deleteEquipmentMessage(@RequestParam(value = "id", required = false) String id) {
        final int i = equipmentMessageInterFace.deleteEquipmentMessage(id);
        if (i > 0)
            return ResultStruct.success(i);
        else
            return ResultStruct.error(ResultVO.class);
    }

    /**
     * 增加门禁机消息与通知，方法ID：IN202004012252589001A8DE9D9BFA9
     *
     * @param communityId 社区ID
     * @param periodId    社区区域ID
     * @param message     消息
     * @param createName  发布者名称
     * @param createTime  通知日期
     * @param sortNo      出现顺序
     * @param beginDate   发布开始日期（不包括当天）
     * @param endDate     发布结束日期（不包括当天）
     * @return 影响条件
     */
    @ApiOperation(value = "增加门禁机消息,方法ID：IN202004012252589001A8DE9D9BFA9", notes = "影响条件")
    @ApiImplicitParams({@ApiImplicitParam(name = "communityId", value = "社区ID", dataType = "String")
            , @ApiImplicitParam(name = "periodId", value = "社区区域ID", dataType = "String")
            , @ApiImplicitParam(name = "message", value = "消息", dataType = "String")
            , @ApiImplicitParam(name = "createName", value = "发布者名称", dataType = "String")
            , @ApiImplicitParam(name = "createTime", value = "通知日期", dataType = "Date")
            , @ApiImplicitParam(name = "sortNo", value = "出现顺序", dataType = "String")
            , @ApiImplicitParam(name = "beginDate", value = "发布开始日期（不包括当天）", dataType = "Date")
            , @ApiImplicitParam(name = "endDate", value = "发布结束日期（不包括当天）", dataType = "Date")
            , @ApiImplicitParam(name = "colorR", value = "R颜色值", dataType = "String", defaultValue = "255", required = true)
            , @ApiImplicitParam(name = "colorG", value = "G颜色值", dataType = "String", defaultValue = "255", required = true)
            , @ApiImplicitParam(name = "colorB", value = "B颜色值", dataType = "String", defaultValue = "255", required = true)
            , @ApiImplicitParam(name = "fontSize", value = "文字大小", dataType = "String", defaultValue = "15", required = true)
    })
    @PostMapping("/insertEquipmentMessage")
    public ResultVO insertEquipmentMessage(@RequestParam(value = "communityId", required = false) String communityId
            , @RequestParam(value = "periodId", required = false) String periodId
            , @RequestParam(value = "message", required = false) String message
            , @RequestParam(value = "createName", required = false) String createName
            , @RequestParam(value = "createTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date createTime
            , @RequestParam(value = "sortNo", required = false) int sortNo
            , @RequestParam(value = "beginDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date beginDate
            , @RequestParam(value = "endDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate
            , @RequestParam(value = "colorR", defaultValue = "-1") int colorR
            , @RequestParam(value = "colorG", defaultValue = "-1") int colorG
            , @RequestParam(value = "colorB", defaultValue = "-1") int colorB
            , @RequestParam(value = "fontSize", defaultValue = "-1") int fontSize
    ) {
        if (beginDate.compareTo(endDate) > 0)
            return ResultStruct.error("发布结束日期不能小于发布开始日期", ResultVO.class);

        final int i = equipmentMessageInterFace.insertEquipmentMessage(communityId, periodId, message, 0
                , createName, createTime, true
                , sortNo, beginDate, endDate
                , colorR, colorG, colorB, fontSize);
        if (i > 0)
            return ResultStruct.success(i);
        else
            return ResultStruct.error(ResultVO.class);
    }

    /**
     * 增加门禁机消息与通知，方法ID：IN202004012252589001A8DE9D9BFA9
     *
     * @param communityId 社区ID
     * @param periodId    社区区域ID
     * @param message     消息
     * @param createName  发布者名称
     * @param createTime  通知日期
     * @param sortNo      出现顺序
     * @param beginDate   发布开始日期（不包括当天）
     * @param endDate     发布结束日期（不包括当天）
     * @return 影响条件
     */
    @ApiOperation(value = "增加门禁机通知(需要审核),方法ID：IN202004012252589001A8DE9D9BFA9", notes = "影响条件")
    @ApiImplicitParams({@ApiImplicitParam(name = "communityId", value = "社区ID", dataType = "String")
            , @ApiImplicitParam(name = "periodId", value = "社区区域ID", dataType = "String")
            , @ApiImplicitParam(name = "message", value = "消息", dataType = "String")
            , @ApiImplicitParam(name = "createName", value = "发布者名称", dataType = "String")
            , @ApiImplicitParam(name = "createTime", value = "通知日期", dataType = "Date")
            , @ApiImplicitParam(name = "sortNo", value = "出现顺序", dataType = "String")
            , @ApiImplicitParam(name = "beginDate", value = "发布开始日期（不包括当天）", dataType = "Date")
            , @ApiImplicitParam(name = "endDate", value = "发布结束日期（不包括当天）", dataType = "Date")
            , @ApiImplicitParam(name = "colorR", value = "R颜色值", dataType = "String", defaultValue = "255", required = true)
            , @ApiImplicitParam(name = "colorG", value = "G颜色值", dataType = "String", defaultValue = "255", required = true)
            , @ApiImplicitParam(name = "colorB", value = "B颜色值", dataType = "String", defaultValue = "255", required = true)
            , @ApiImplicitParam(name = "fontSize", value = "文字大小", dataType = "String", defaultValue = "15", required = true)
    })
    @PostMapping("/insertEquipmentMessageAudio")
    public ResultVO insertEquipmentMessageAudio(@RequestParam(value = "communityId", required = false) String communityId
            , @RequestParam(value = "periodId", required = false) String periodId
            , @RequestParam(value = "message", required = false) String message
            , @RequestParam(value = "createName", required = false) String createName
            , @RequestParam(value = "createTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date createTime
            , @RequestParam(value = "sortNo", required = false) int sortNo
            , @RequestParam(value = "beginDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date beginDate
            , @RequestParam(value = "endDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate
            , @RequestParam(value = "colorR", defaultValue = "-1") int colorR
            , @RequestParam(value = "colorG", defaultValue = "-1") int colorG
            , @RequestParam(value = "colorB", defaultValue = "-1") int colorB
            , @RequestParam(value = "fontSize", defaultValue = "-1") int fontSize
    ) {
        if (beginDate.compareTo(endDate) > 0)
            return ResultStruct.error("发布结束日期不能小于发布开始日期", ResultVO.class);
        final int i = equipmentMessageInterFace.insertEquipmentMessage(communityId, periodId, message
                , 1, createName, createTime, false
                , sortNo, beginDate, endDate
                , colorR, colorG, colorR, fontSize
        );
        if (i > 0)
            return ResultStruct.success(i);
        else
            return ResultStruct.error(ResultVO.class);
    }

    /**
     * 根据设备SN号得到消息与通知信息，方法ID：SE20200401214022506AF10BC0583F0
     *
     * @param sn 设备sn
     * @return 设备基本信息
     */
    @ApiOperation(value = "根据设备SN号得到消息与通知信息,方法ID：SE20200401214022506AF10BC0583F0", notes = "设备基本信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "useId", value = "设备SN", dataType = "String", defaultValue = "sn123456", required = true)
    })
    @PostMapping("/selectEquipmentMessageBySn")
    public ResultVO selectEquipmentMessageBySn(@RequestParam(value = "useId") String sn) {
        String communityId = null;
        String periodId = null;
        final ResultVO resultVOEquipSite = equipmentInterFace.selectEquipmentSiteBySn(sn);
        final List list = gson.fromJson(resultVOEquipSite.getData().toString(), List.class);
        for (Object row :
                list) {
            final EquipmentSiteModel equipmentSiteModel = gson.fromJson(row.toString(), EquipmentSiteModel.class);
            switch (equipmentSiteModel.getRoomType()) {
                case 0:
                    communityId = equipmentSiteModel.getSiteId();//场所
                    break;
                case 1:
                    periodId = equipmentSiteModel.getSiteId();//区域
                    break;
            }
        }
        if (periodId != null && communityId != null)
            return equipmentMessageInterFace.selectEquipmentMessageBySn(communityId, periodId);
        else
            return ResultStruct.error("无数据", ResultVO.class);
    }

}
